package com.android.happymall.fragment.member;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.happymall.R;
import com.android.happymall.fragment.BaseLazyFragment;
import com.android.happymall.model.member.CoinDetailList;
import com.android.happymall.model.member.CoinDetailListCallback;
import com.android.happymall.utils.ConstantsUrl;
import com.android.happymall.utils.HttpUtils;
import com.android.happymall.utils.MyLog;
import com.android.happymall.utils.ObjectUtils;
import com.android.happymall.view.recycleview.CustomLoadMoreView;
import com.android.happymall.view.recycleview.CustomRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;


/**
 * 趣豆明细列表
 */
public class Fragment_Mine_Coin_Detail_Tab extends BaseLazyFragment implements BaseQuickAdapter.RequestLoadMoreListener {

    @BindView(R.id.top)
    ImageView top;
    @BindView(R.id.listView)
    CustomRecycleView mRecyclerView;
    @BindView(R.id.swipe_refresh_layout)
    SwipeRefreshLayout mRefreshLayout;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.tv_norecord)
    TextView noRecord;
    @BindView(R.id.detail_rate_tip)
    RelativeLayout detailRateTip;
    private FragmentActivity mcontext;
    private View view;
    private static final String ARG_PARAM1 = "param1";
    private String mParam1;
    private boolean networkConnected;
    private int page = 1;
    private ScoreDetailAdapt adapter;

    public Fragment_Mine_Coin_Detail_Tab() {


    }

    public static Fragment_Mine_Coin_Detail_Tab newInstance(String param1) {
        Fragment_Mine_Coin_Detail_Tab fragment = new Fragment_Mine_Coin_Detail_Tab();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
        }
    }

    @Override
    protected void initPrepare() {
        ReFreshData();
    }

    private void initRecycle() {
        adapter = new ScoreDetailAdapt(R.layout.item_score_detail_list, null);
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.setOnLoadMoreListener(this, mRecyclerView);
        LinearLayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.GetToTop(top, 0);
    }

    @Override
    protected void onInvisible() {
    }


    @Override
    protected void initData() {
        networkConnected = ObjectUtils.isNetworkConnected(getActivity());
        if (networkConnected) {
            getCoinData(page, mParam1);
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
        }
    }

    @Override
    protected View initView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_directbuy_goodsdtail_comment, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        noRecord.setText("没有数据");
        initRecycle();
        mRecyclerView.setBackgroundColor(Color.WHITE);
        return view;
    }

    private void ReFreshData() {
        mRefreshLayout.setColorSchemeResources(R.color.app_title);
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                networkConnected = ObjectUtils.isNetworkConnected(mcontext);
                if (networkConnected) {
                    page = 1;
                    getCoinData(page, mParam1);
                } else {
                    ObjectUtils.toast(mcontext, "网络连接出现异常");
                    mRefreshLayout.setRefreshing(false);
                }
            }
        });
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick() {
        ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
        page = 1;
        getCoinData(page, mParam1);
    }

    @Override
    public void onLoadMoreRequested() {
        page = page + 1;
        getCoinData(page, mParam1);
    }

    private void getCoinData(final int page, String status) {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.MEMBER_COIN;
            MyLog.e("地址", address);
            HashMap<String, String> map = new HashMap<>();
            map.put("page", page + "");
            map.put("status", status + "");
            HashMap<String, String> header = ObjectUtils.getHeader(mcontext);
            HttpUtils.get(address, header, map, dataCallback);
        } else {
            if (page == 1) {
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
            }
            ObjectUtils.toast(getActivity(), "网络连接出现异常");
            adapter.loadMoreFail();
        }
    }

    CoinDetailListCallback dataCallback = new CoinDetailListCallback() {

        @Override
        public void onError(Call call, Exception e, int id) {
            mRefreshLayout.setRefreshing(false);
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
        }

        @Override
        public void onResponse(CoinDetailList response, int id) {
            mRefreshLayout.setRefreshing(false);
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
            if (response.getCode() == 0) {
                ArrayList<CoinDetailList.ListBean> list_comment = new ArrayList<>();
                if (response.getList() == null && page == 1) {
                    detailRateTip.setVisibility(View.VISIBLE);
                    return;
                }
                if (response.getList() != null && response.getList().size()>0) {
                    list_comment.addAll(response.getList());
                    if (page == 1) {
                        adapter.setNewData(list_comment);
                    } else {
                        adapter.addData(list_comment);
                        adapter.loadMoreComplete();
                    }
                    detailRateTip.setVisibility(View.GONE);
                } else {
                    adapter.loadMoreEnd();
                }
            } else {
                noData.setVisibility(View.VISIBLE);
                noDataTv.setText(response.getMsg());
                noDataTv.setVisibility(View.VISIBLE);
            }
        }
    };

    private class ScoreDetailAdapt extends BaseQuickAdapter<CoinDetailList.ListBean, BaseViewHolder> {

        public ScoreDetailAdapt(int layoutResId, List<CoinDetailList.ListBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, CoinDetailList.ListBean data) {
            holder.setText(R.id.item_score_detail_remark, data.getRemark());
            holder.setText(R.id.item_score_detail_time, data.getC_time());
            String type = data.getType();
            if(mParam1.equals("1")){
                if(type.equals("0")){
                    holder.setText(R.id.tv_num, "+"+data.getAmount()+"趣豆");
                }else{
                    holder.setText(R.id.tv_num, "+"+data.getAmount()+"赠豆");
                }
            }else {
                if(type.equals("0")){
                    holder.setText(R.id.tv_num, data.getAmount()+"趣豆");
                }else{
                    holder.setText(R.id.tv_num, data.getAmount()+"赠豆");
                }
            }
            if(holder.getLayoutPosition()%2 == 0){
                holder.setBackgroundColor(R.id.item_score_detail_ll, Color.WHITE);
            }else {
                holder.setBackgroundColor(R.id.item_score_detail_ll,Color.parseColor("#eeeeee"));
            }
        }
    }
}
